//
//  PersonalInformationViewController.swift
//  SwiftOfXiaoShouJia
//
//  Created by zhenjingxiong on 2018/9/6.
//  Copyright © 2018年 镇景雄. All rights reserved.
//

import UIKit

class PersonalInformationViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
   
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "个人信息"
        
        self.layoutUI()
        
    }
    
    
    func layoutUI() {
        self.view.addSubview(self.listView)
        self.listView.tableFooterView = nil
        self.listView.tableFooterView = self.loginView
        self.loginView.addSubview(self.loginButton)
        self.loginButton.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsMake(10, 10, 0, 10))
        }
        
        self.listView.register(PersonalInformationFirstCell.self, forCellReuseIdentifier: "PersonalInformationFirst")
        self.listView.register(PersonalInformationSecondCell.self, forCellReuseIdentifier: "PersonalInformationSecond")
    }
    
    lazy var listView: UITableView = {
        let listView = UITableView.init(frame: self.view.bounds)
        listView.delegate = self
        listView.dataSource = self
        listView.sectionFooterHeight = 0.01
        listView.sectionHeaderHeight = 0.01
        listView.backgroundColor = UIColor.withHex(hexString: "#eeeeee")

        return listView
    }()
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 4
        default:
            return 1
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 15
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 80
        }else{
            return 50
        }
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        view.tintColor = UIColor.withHex(hexString: "#eeeeee")
    }

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            let firstCell:PersonalInformationFirstCell = tableView.dequeueReusableCell(withIdentifier: "PersonalInformationFirst") as! PersonalInformationFirstCell
//            firstCell.iconImageView.image = self.personalImage
            return firstCell
        }else{
            let secondCell:PersonalInformationSecondCell = tableView.dequeueReusableCell(withIdentifier: "PersonalInformationSecond") as! PersonalInformationSecondCell
            if indexPath.section == 1 {
                secondCell.leftLabel.text = self.leftLabalArray[indexPath.row] as? String
                secondCell.rightLabel.text = self.rightLabelArray[indexPath.row] as? String
            }else{
                secondCell.leftLabel.text = "修改密码"
                secondCell.rightLabel.text = ""
            }
            return secondCell
        }
        
    }
    
    lazy var loginView: UIView = {
        let loginView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 55))
        loginView.backgroundColor = UIColor.clear
        return loginView
    }()
    
    lazy var loginButton: UIButton = {
        let loginButton = UIButton.init()
        loginButton.setTitle("退出登录", for: UIControlState.normal)
        loginButton.setTitleColor(UIColor.white, for: UIControlState.normal)
        loginButton.backgroundColor = UIColor.red
        return loginButton
    }()
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch indexPath.section {
        case 0:
            self.present(self.imagePicker, animated: true, completion: nil)
            
        case 1:
            print("点击第二区域")
            switch indexPath.row {
            case 0:
                let MC = ModifyNameViewController()
                self.navigationController?.pushViewController(MC, animated: true)
                MC.backClosure = {
                    (backStr: String) -> Void in
                      let sencondCell:PersonalInformationSecondCell = self.listView.cellForRow(at: NSIndexPath.init(row: 0, section: 1) as IndexPath) as! PersonalInformationSecondCell
                    sencondCell.rightLabel.text = backStr
                    print("%@",backStr);
                }
                
                MC.getValue = {
                    (getBool: Bool) -> Void in
                    print("%ld",getBool)
                }

            default:
                print("打印其他的")
            }
        default:
            print("修改密码")
        }
    }


    
    
    lazy var imagePicker: UIImagePickerController = {
        let imagePicker = UIImagePickerController.init()
        // 表示操作为进入系统相册
        imagePicker.sourceType = .photoLibrary
        // 设置代理为 ViewController
        imagePicker.delegate = self
        // 允许用户编辑选择的图片
        imagePicker.allowsEditing = true
        // 进入系统相册界面
        return imagePicker
    }()
    
     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        let pickedImage:UIImage = info[UIImagePickerControllerEditedImage] as! UIImage
        let firstCell:PersonalInformationFirstCell = self.listView.cellForRow(at: NSIndexPath.init(row: 0, section: 0) as IndexPath) as! PersonalInformationFirstCell
        firstCell.iconImageView.image = pickedImage
        firstCell.headPortraitLabel.text = "我的头像"

        print("%@",Thread.current)
        
        // 必须写这行，否则拍照后点击重新拍摄或使用时没有返回效果。
        picker.dismiss(animated: true, completion: nil)
        
    }
    
    lazy var personalImage: UIImage = {
        let personalImage = UIImage.init(named: "avatar_default")
        return personalImage!
    }()
    
    lazy var leftLabalArray: NSArray = {
        let leftLabalArray = ["姓名","性别","个人微信号","个人微信二维码"]
        return leftLabalArray as NSArray
    }()
    
    lazy var rightLabelArray: NSArray = {
        let rightLabelArray = ["镇景雄","","未上传",""]
        return rightLabelArray as NSArray
    }()
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
